﻿using System.Windows.Forms;

using Pibby.Core;
using System.Collections.Generic;

namespace Pibby.Controls
{
	public class PodcastListView : SortableListView
	{
		public PodcastListView()
			: base()
		{
		}

		public void Configure()
		{
			this.View = View.Details;

			this.Columns.Add("Podcast", 100, HorizontalAlignment.Left);
			this.Columns.Add("Last Updated", 100, HorizontalAlignment.Left);
			this.Columns.Add("Whuffie", 100, HorizontalAlignment.Right);
            this.Columns.Add("Description", 100, HorizontalAlignment.Left);
			this.Columns.Add("Url", 100, HorizontalAlignment.Left);

			this.RefreshColumnSpacing();
	
			this.FullRowSelect = true;
		}

		public void RefreshColumnSpacing()
		{
			int columnWidth = this.ClientSize.Width / 10;

			if (this.Columns.Count == 5)
			{
				this.Columns[0].Width = columnWidth * 4;
				this.Columns[1].Width = columnWidth * 2;
				this.Columns[2].Width = columnWidth;
				this.Columns[3].Width = columnWidth;
				this.Columns[4].Width = columnWidth * 2;
			}
		}

		public void UpdateContent(PodcastCollection collection)
		{
			List<ListViewItem> items = new List<ListViewItem>();

			foreach (Podcast p in collection)
			{
				items.Add(new PodcastListViewItem(p));
			}

			base.UpdateContent(items);
		}

		public void RefreshContent(PodcastCollection collection)
		{
			this.Items.Clear();

			this.UpdateContent(collection);

			this.SortByColumn(1, SortOrder.Descending);
		}
	}
}

